/*
 * Copyright 2008-2012 WebEqua
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mathcloud.math.plotting.maple;

import java.io.File;

import org.mathcloud.math.core.maple.factory.MapleQuantityObjectFactory;
import org.mathcloud.math.core.maple.manager.MapleSupervisor;
import org.mathcloud.math.core.maple.quantity.MapleQuantityObject;
import org.mathcloud.math.core.maple.quantity.MapleRealMatrix;
import org.mathcloud.math.io.maple.MapleQuantityObjectBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.mathcloud.math.core.common.quantity.RealMatrix;

import com.maplesoft.externalcall.MapleException;

public class MaplePlot 
{
	public MaplePlot()
	{
		
	}
	
	public File plotData(RealMatrix data, String fileOut)
	{
		try
		{
			MapleQuantityObject mdata = MapleQuantityObjectFactory.getInstance(data);
			String mapleJob = "plotsetup(bmp,plotoutput=\""+fileOut+"\");";
			mapleJob = mapleJob + "plot("+mdata.getVarName()+");";
			log.info(mapleJob);
			MapleSupervisor.getMapleEngine().evaluate(mapleJob);
    	File res = new File(fileOut);
    	if (res.exists())
    		return res;
    	else return null;
		} catch (MapleException e)
		{
			log.error("Error plotting data - ", e);
			return null;
		}				
	}	
	
	public File plotData(String dataFile, String fileOutput)
	{
		MapleQuantityObjectBuilder in = new MapleQuantityObjectBuilder();
		RealMatrix real = in.readRealMatrix(dataFile, 2);
		if (real!=null)
			return plotData(real, fileOutput);
		else return null;
	}

	public File plotPoints(double[][] arg0, String fileOutput)
	{
		MapleRealMatrix data = new MapleRealMatrix(arg0);
		return plotData(data, fileOutput);
	}

	public File plotExpression(String expr, String fileOut)
	{
		try
		{
			String mapleJob = "plotsetup(bmp,plotoutput=\""+fileOut+"\");";
			mapleJob = mapleJob + "plot("+expr+");";			
			log.info(mapleJob);
			MapleSupervisor.getMapleEngine().evaluate(mapleJob);
    	File res = new File(fileOut);
    	if (res.exists())
    		return res;
    	else return null;
		} catch (MapleException e)
		{
			log.error("Error plotting data - ", e);
			return null;
		}				
	}
	
	public void plot3D(String tmp, String funct, String fileOutput)
	{
		
		try
		{
			File f = new File(fileOutput+"."+tmp);
			f.createNewFile();
			StringBuilder mapleJob = new StringBuilder("plotsetup(");
			mapleJob.append(tmp);
			mapleJob.append(",plotoutput=`");
			String str = f.getCanonicalPath().replace("\\", "//");
			mapleJob.append(str);
			mapleJob.append("`);plot3d(");
			mapleJob.append(funct);
			mapleJob.append(", title = `Test Graph`);");
			log.info(mapleJob.toString());
			MapleSupervisor.getMapleEngine().evaluate(mapleJob.toString());
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			log.error("Error plotting data - ", e);
		}		
	}
		
	private String options;
	private static final Logger log = LoggerFactory.getLogger(MaplePlot.class);
}
